﻿
using Dapper;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCEntity.CRM.BigCustomerGroup;

namespace BCData.CRM.BigCustomerGroup
{
    public class BigCustomerGroupData : IBigCustomerGroupData
    {
        private readonly IDatabaseContext database;
        private readonly string QUERY = @"SELECT
		    A.BigCustomerGroupId,
		    A.CompanyId,
		    A.BigCustomerGroupName,
		    A.GroupRemark,
		    A.CreateTime,
		    A.CreateAdminId,
		    A.CreateAdminName,
		    A.UpdateTime,
		    A.EditAdminId,
		    A.EditAdminName
        FROM
            CRM_BigCustomerGroup A";



        #region 构造函数
        public BigCustomerGroupData(IDatabaseContext database)
        {
            this.database = database;
        }
        #endregion

        #region InsertAsync
        public async Task<BigCustomerGroupEntity> InsertAsync(BigCustomerGroupEntity entity)
        {
            return await this.database.Master.InsertAsync(entity, database.Transaction);
        }
        #endregion

        #region updateAsync
        public async Task<BigCustomerGroupEntity> UpdateAsync(BigCustomerGroupEntity entity)
        {
            return await this.database.Master.UpdateAsync(entity, database.Transaction);
        }
        #endregion 

        #region Delete
        public async Task<bool> DeleteAsync(long bigCustomerGroupId)
        {
            string sql = "DELETE FROM crm_bigcustomergroup WHERE BigCustomerGroupId = ?bigCustomerGroupId";
            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BigCustomerGroupId", bigCustomerGroupId, DbType.Int64);
            return await this.database.Master.ExecuteAsync(sql, parameters, database.Transaction) > 0;
        }
        #endregion

        #region GetById
        public async Task<BigCustomerGroupEntity> GetEntityAsync(long bigCustomerGroupId)
        {
            string sql = QUERY + " WHERE  A.BigCustomerGroupId = ?bigCustomerGroupId";

            DynamicParameters parameters = new DynamicParameters();
            parameters.Add("?BigCustomerGroupId", bigCustomerGroupId, DbType.Int64);
            return await this.database.Slave.QueryEntityAsync<BigCustomerGroupEntity>(sql, parameters);
        }
        #endregion

        #region GetListAsync 查询大客户组记录
        public async Task<(IEnumerable<BigCustomerGroupEntity>, int)> GetListAsync(int? companyId,string bigCustomerGroupName,  int pageIndex, int pageSize)
        {

            DynamicParameters parameters = new DynamicParameters();
            //查询条件
            List<string> wheres = new List<string>();

            if (companyId.HasValue)
            {
                wheres.Add("CompanyId=?CompanyId");
                parameters.Add("?CompanyId", companyId, DbType.Int64);
            }

            if(!string.IsNullOrWhiteSpace(bigCustomerGroupName))
            {
                wheres.Add("BigCustomerGroupName LIKE ?BigCustomerGroupName");
                parameters.Add("?BigCustomerGroupName", $"%{bigCustomerGroupName.Trim()}%",DbType.AnsiString);
            }
            string sql = string.Format(@"{0} {1} LIMIT {2},{3} ; SELECT COUNT(1) FROM  crm_bigcustomergroup  {1}",
                   QUERY,
                  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
                  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
                  pageSize
                  ) ;

            return await this.database.Slave.QueryPageableListAsync<BigCustomerGroupEntity>(sql, parameters);
        }
        #endregion

        #region 查询GetOnlyList

        public async Task<IEnumerable<BigCustomerGroupEntity>> GetOnlyList(int? companyId)
        {

            DynamicParameters parameters = new DynamicParameters();
            //查询条件
            List<string> wheres = new List<string>();

            if (companyId.HasValue)
            {
                wheres.Add("CompanyId=?CompanyId");
                parameters.Add("?CompanyId", companyId, DbType.Int64);
            }
            string sqlWhere =   wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "";

            return await this.database.Slave.QueryAsync<BigCustomerGroupEntity>(QUERY+sqlWhere, parameters);
        }
        #endregion
    }
}
//接口



